<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <div class="shuxing">
        <el-form-item label="行政区域" prop="areaId">
          <treeselect
            class="shuxing1"
            v-model="queryParams.areaId"
            :options="areaList"
            :normalizer="normalizer"
            placeholder="选择区域"
          />
        </el-form-item>
      </div>
      <el-form-item label="主体名称" prop="enterName">
        <el-input
          v-model="queryParams.enterName"
          placeholder="请输入主题名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="130px" label="购买投入品名称" prop="enterName">
        <el-input
          v-model="queryParams.purchaseName"
          placeholder="请输入购买投入品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['purchase:purchaseInfo:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['purchase:purchaseInfo:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="purchaseInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        :index="indexMethod"
        align="center"
        label="序号"
      >
      </el-table-column>
      <el-table-column label="主体名称" align="center" prop="enterName" />
      <el-table-column
        label="购买产品名称"
        align="center"
        prop="purchaseName"
      />
      <el-table-column label="购买数量" align="center" prop="purchaseNumber" />
      <el-table-column label="购买单位" align="center" prop="purchaseUnit" />
      <el-table-column label="购买日期" align="center" prop="purchaseTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['purchase:purchaseInfo:edit']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投入品购买信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="200px" :disabled="show" >
         <el-form-item label="主体名称" prop="enterId">
         <!--  <el-input v-model="form.enterId" placeholder="请输入主题编号" /> -->
          <el-select style="width: 580px;"
                v-model="form.enterId"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in EnterpriseInfoList"
                  :key="item.id"
                  :label="item.enterName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
        </el-form-item>

        <!-- <el-form-item label="投入品类型" prop="batchNum" >
          <el-input v-model="form.purchaseType" placeholder="请输入投入品类型" />
        </el-form-item>
        <el-form-item label="种植物类型" prop="purchaseTime"  >
          <el-input v-model="form.plantType" placeholder="请输入种植物类型" />
        </el-form-item> -->

        <el-form-item label="投入品类型">
          <el-select v-model="form.purchaseType" :placeholder="form.purchaseType?form.purchaseType:'请选择投入品类型'">
            <el-option label="种子" value="01"></el-option>
            <el-option label="农药" value="02"></el-option>
              <el-option label="农药" value="03"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="种植物类型">
          <el-select v-model="form.plantType"  :placeholder="form.plantType?form.plantType:'请选择种植物类型'" >
            <el-option label="稻" value="稻"></el-option>
            <el-option label="小麦" value="小麦"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="批次号" prop="purchaseName">
          <el-input v-model="form.batchNum" placeholder="请输入批次号" minlength="32"/>
        </el-form-item>
        <el-form-item label="购买日期" prop="purchaseNumber">
          <el-input v-model="form.purchaseTime" placeholder="请输入购买日期" />
        </el-form-item>
        <el-form-item label="购买投入品名称" prop="purchaseUnit">
          <el-input
            v-model="form.purchaseName"
            placeholder="请输入购买投入品名称"
          />
        </el-form-item>
        <el-form-item label="购买数量" prop="traceCode">
          <el-input
            v-model="form.purchaseNumber"
            placeholder="请输入购买数量"
          />
        </el-form-item>
        <el-form-item label="购买单位" prop="traceCode">
          <el-input v-model="form.purchaseUnit" placeholder="请输入购买单位" />
        </el-form-item>
        <el-form-item label="投入品追溯码" prop="traceCode">
          <el-input v-model="form.traceCode" placeholder="请输入投入品追溯码" />
        </el-form-item>

        <!-- <el-form-item label="投入品经营主体" prop="traceCode">
          <el-input
            v-model="form.enterName"
            placeholder="请输入投入品经营主体"
          />
        </el-form-item> -->

        <el-form-item label="投入品经营主体">
          <el-select v-model="form.purchaseEnterName" :placeholder="form.purchaseEnterName?form.purchaseEnterName:'请选择投入品经营主体'" >
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="投入品经营主体社会信用代码" prop="traceCode">
          <el-input
            v-model="form.purchaseSocialCreditCode"
            placeholder="请输入投入品经营主体社会信用代码"
          />
        </el-form-item>
        <el-form-item label="投入品经营主体身份码" prop="traceCode">
          <el-input
            v-model="form.purchaseEnterIdCard"
            placeholder="请输入投入品经营主体身份码"
          />
        </el-form-item>
        <el-form-item label="主体备案地址" prop="traceCode">
          <el-input v-model="form.enterArea" placeholder="请输入主体备案地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPurchaseInfo,
  getPurchaseInfo,
  delPurchaseInfo,
  addPurchaseInfo,
  updatePurchaseInfo,
  exportPurchaseInfo,
} from "@/api/purchase/purchaseInfo";
import {
 getTree,listEnterpriseInfo
} from "@/api/enterpriseInfo/EnterpriseInfo";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "PurchaseInfo",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,

      areaList: [],
      // 投入品购买信息表格数据
      purchaseInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaId: undefined,
        purchaseType: undefined,
        batchNum: undefined,
        purchaseTime: undefined,
        purchaseName: undefined,
        purchaseNumber: undefined,
        purchaseUnit: undefined,
        traceCode: undefined,
        enterId: undefined,
        enterName: undefined,
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 1000,
        enterName: undefined,
        enterArea: undefined,
        plantId: undefined,
        orgId: undefined,
        sysProId: undefined,
        mainCertificeType: undefined,
        enterAddress: undefined,
        socialCreditCode: undefined,
        productOperationType: undefined,
        longitude: undefined,
        dimension: undefined,
        industry: undefined,
        enterIdcard: undefined,
        enterImg: undefined,
        enterLicenseImg: undefined,
        createDate: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      EnterpriseInfoList: [],
    };
  },
  created() {
    this.getList();
    this.getTreeList();
    this.getList11();
  },
  methods: {
    /** 查询投入品购买信息列表 */
    getList() {
      this.loading = true;
      listPurchaseInfo(this.queryParams).then((response) => {
        this.purchaseInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getTreeList() {
       getTree().then(response => {
	        this.areaList = this.handleTree(response.data, "id");
      });
    },
     /** 查询种植主体管理列表 */
    getList11() {
      listEnterpriseInfo(this.queryParams1).then((response) => {
        this.EnterpriseInfoList = response.rows;
        this.loading = false;
      });
    },
     /** 转换部门数据结构 */
    normalizer(node) {
      console.log(JSON.stringify(node)+"11111111111111111111111111111111111111111111111111111")
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children
      };
    },
    // 序号
    indexMethod(index) {
      return index + 1;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        purchaseType: undefined,
        batchNum: undefined,
        purchaseTime: undefined,
        purchaseName: undefined,
        purchaseNumber: undefined,
        purchaseUnit: undefined,
        traceCode: undefined,
        enterId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.purchaseName = "";
      this.queryParams.enterName = "";
      // this.resetForm("queryForm");
      // this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getList11();
      this.reset();
      this.show = false;
      this.open = true;
      this.title = "添加投入品购买信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getList11();
      this.reset();
      this.show = true;
      const id = row.id || this.ids;
      getPurchaseInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "投入品购买信息详情";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            this.open = false;
            this.getList();
            // updatePurchaseInfo(this.form).then(response => {
            //   if (response.code == 0) {
            //     // this.msgSuccess("");
            //     this.open = false;
            //     this.getList();
            //   }
            // });
          } else {
            addPurchaseInfo(this.form).then((response) => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除投入品购买信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delPurchaseInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有投入品购买信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportPurchaseInfo(queryParams);
        })
        .then((response) => {
          this.download(response.data);
        })
        .catch(function () {});
    },
  },
};
</script>
<style lang="scss" scoped>

.shuxing {
  width: 250px;
  float: left;
  .shuxing1{
    width: 150px !important;
    height: 35px;
  }
  
}
</style>